package JF;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import YS.App;

//登录窗体类 
public class denglu extends JFrame implements ActionListener, Runnable,
		MouseListener {
	private static final long serialVersionUID = 1L;
	protected JLabel lblUserName, lblPassword;
	protected JTextField txtUserName;
	protected JPasswordField txtPassword;
	protected JButton btnLogin, btnRegister;
	protected static Thread thread = null;

	public static void main(String[] args) {
		Data.init();
		denglu frmLogin = new denglu();
		thread = new Thread(frmLogin);
		thread.start();

	}

	public denglu() {
		super("用户登录");
		initComponent();
	}

	// 初始化控件
	public void initComponent() {
		lblUserName = new JLabel("  用        户       名:");
		lblPassword = new JLabel("  密                  码:");
		txtUserName = new JTextField("请输入用户名");
		txtUserName.setForeground(Color.gray);
		txtUserName.addMouseListener(this);
		txtPassword = new JPasswordField("");
		btnLogin = new JButton("登录");
		btnRegister = new JButton("注册");
		btnLogin.addActionListener(this);
		btnRegister.addActionListener(this);

		JPanel panel = (JPanel) this.getContentPane();
		panel.setLayout(null);
		panel.setBackground(Color.cyan);

		lblUserName.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(lblUserName);

		lblPassword.setBounds(200, 60, 100, 30);
		lblPassword.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(lblPassword);

		txtUserName.setBounds(100, 120, 100, 30);
		txtUserName.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(txtUserName);

		txtPassword.setBounds(200, 120, 100, 30);
		txtPassword.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(txtPassword);

		btnLogin.setForeground(Color.blue);
		btnLogin.setFont(new Font(null, Font.BOLD, 20));
		panel.add(btnLogin);

		btnRegister.setBounds(210, 180, 100, 50);
		btnRegister.setForeground(Color.red);
		btnRegister.setFont(new Font(null, Font.BOLD, 20));
		this.setVisible(true);
		this.setLayout(new GridLayout(3, 2));
		this.add(lblUserName);
		this.add(txtUserName);
		this.add(lblPassword);
		this.add(txtPassword);
		this.add(btnLogin);
		this.add(btnRegister);
		txtUserName.setFocusable(true);
		this.setLocationRelativeTo(null);// 窗体居中
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(400, 200);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton btn = (JButton) e.getSource();
		if (btn == btnLogin) {
			if (txtUserName.getText().trim().equals("")
					|| txtUserName.getText().trim().equals("请输入用户名")) {
				JOptionPane.showMessageDialog(this, "用户名不能为空！", "登录失败",
						JOptionPane.ERROR_MESSAGE);
				return;
			}
			if (txtPassword.getText().equals("")) {
				JOptionPane.showMessageDialog(this, "登录密码不能为空！", "登录失败",
						JOptionPane.ERROR_MESSAGE);
				return;
			}
			String userName = txtUserName.getText().trim();
			String password = String.valueOf(txtPassword.getPassword());

			if (Test.login(userName, password)) { // 判斷用戶名和密碼是否都正確

				JOptionPane.showMessageDialog(this, "欢迎您，" + userName,
						"您已登录成功", JOptionPane.INFORMATION_MESSAGE);
				new App();
				this.setVisible(false);
			} else {
				JOptionPane.showMessageDialog(this,
						"登录失败，请检查输入的用户名和密码是否正确......", "登录失败",
						JOptionPane.ERROR_MESSAGE);
			}
		} else if (btn == btnRegister) {
			this.dispose();
			new yonghu();
		}
	}

	public void run() {

	}

	@Override
	public void mouseClicked(MouseEvent e) {

		if (e.getSource() == txtUserName) {
			txtUserName.setText(null);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}
}

// 用户注册窗体类
class yonghu extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	protected JLabel lblUserName, lblPassword, lblConfirmedPassword;
	protected JTextField txtUserName;
	protected JPasswordField txtPassword, txtConfirmedPassword;
	protected JButton btnRegister, btnReset;

	public yonghu() {
		super("用户注册");
		initComponent();
	}

	// 初始化控件
	public void initComponent() {
		lblUserName = new JLabel("   用       户       名:");
		lblPassword = new JLabel("   密                 码:");
		lblConfirmedPassword = new JLabel("  确    认   密    码:");
		txtUserName = new JTextField(5); // 限定用户名不超过5个字符
		txtPassword = new JPasswordField(16); // 限定密码不超过16个字符
		txtConfirmedPassword = new JPasswordField(5);
		btnReset = new JButton("重置");
		btnRegister = new JButton("注册");
		btnReset.addActionListener(this);
		btnRegister.addActionListener(this);

		JPanel panel = (JPanel) this.getContentPane();
		panel.setLayout(null);
		panel.setBackground(Color.magenta);
		panel.add(btnRegister);

		lblUserName.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(lblUserName);
		lblUserName.setBounds(200, 60, 100, 30);
		lblPassword.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(lblPassword);
		lblConfirmedPassword.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(lblConfirmedPassword);
		lblUserName.setBounds(200, 120, 100, 30);
		txtPassword.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(txtPassword);
		lblPassword.setBounds(200, 120, 100, 30);
		txtPassword.setFont(new Font(null, Font.ITALIC, 20));
		panel.add(btnRegister);

		btnRegister.setForeground(Color.blue);
		btnRegister.setFont(new Font(null, Font.BOLD, 20));
		panel.add(btnRegister);
		btnRegister.setBounds(210, 180, 100, 50);

		btnReset.setForeground(Color.red);
		btnReset.setFont(new Font(null, Font.BOLD, 20));
		panel.add(btnReset);
		this.setVisible(true);

		this.setLayout(new GridLayout(4, 2));
		this.add(lblUserName);
		this.add(txtUserName);
		this.add(lblPassword);
		this.add(txtPassword);
		this.add(lblConfirmedPassword);
		this.add(txtConfirmedPassword);
		this.add(btnRegister);
		this.add(btnReset);
		txtUserName.setFocusable(true);
		this.setSize(400, 200);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		JButton btn = (JButton) e.getSource();
		if (btn == btnRegister) {
			if (txtUserName.getText().equals("")) {
				JOptionPane.showMessageDialog(this, "用户名不能为空！", "注册失败",
						JOptionPane.ERROR_MESSAGE);

				return;

			}

			if (txtPassword.getText().equals("")) {

				JOptionPane.showMessageDialog(this, "登录密码不能为空！", "注册失败",
						JOptionPane.ERROR_MESSAGE);

				return;

			}

			if (txtConfirmedPassword.getText().equals("")) {

				JOptionPane.showMessageDialog(this, "确证密码不能为空！", "注册失败",
						JOptionPane.ERROR_MESSAGE);

				return;

			}

			if (!txtConfirmedPassword.getText().equals(txtPassword.getText())) {

				JOptionPane.showMessageDialog(this, "两次密码必须一致！", "注册失败",
						JOptionPane.ERROR_MESSAGE);

				return;

			}

			String userName = null;

			String password = null;

			int i;

			userName = txtUserName.getText();

			password = txtPassword.getText();

			if (!Test.regist(userName, password)) {

				JOptionPane.showMessageDialog(this, "系統異常",// "该用户名已经被注册，请选用其他用户名！",
						"注册失败", JOptionPane.ERROR_MESSAGE);

			}

			else {

				Data.customers.add(new Customer(userName, password));

				JOptionPane.showMessageDialog(this, "恭喜 " + userName
						+ " 注册成功，请牢记您的用户名和密码。\n单击\"确定\"按钮开始登录", "注册成功",
						JOptionPane.INFORMATION_MESSAGE);

				this.dispose();

				new denglu();

			}

		}

		else if (btn == btnReset) {

			txtUserName.setText("");

			txtPassword.setText("");

			txtConfirmedPassword.setText("");

			txtUserName.setFocusable(true);

		}

	}

}

// 用户信息类

class Customer {

	protected String userName = null;

	protected String password = null;

	public Customer(String userName, String password) {

		this.userName = userName;

		this.password = password;

	}

	public String getUserName() {

		return this.userName;

	}

	public void setUserName(String userName) {

		this.userName = userName;

	}

	public String getPassword() {

		return this.password;

	}

	public void setPassword(String password) {

		this.password = password;

	}

}

// 缓存用户信息的集合类

class Data {

	public static List<Customer> customers = new ArrayList<Customer>();

	public static void init() {

		customers.add(new Customer("fxk", "123456"));

	}

}